import 'package:flutter_demo2/leak/leak_watcher/src/leak/model/leak_node.dart';

class LeakInfo {
  String? time;
  int? occurCount = 1;
  LeakNode? node;

  LeakInfo(this.time, this.node, this.occurCount);

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['time'] = time;
    if (node != null) {
      data['node'] = node!.toJson();
    }
    data['occurCount'] = occurCount;
    return data;
  }

  LeakInfo.fromJson(Map<String, dynamic> json) {
    time = json['time'];
    node = json['node'] != null ? LeakNode.fromJson(json['node']) : null;
    occurCount = json['occurCount'];
  }

  bool equal2another(LeakInfo other) {
    final currentNodeList = toList(node);
    final otherNodeList = toList(other.node);
    if (currentNodeList.length != otherNodeList.length) return false;
    for (int i = 0; i < currentNodeList.length; i++) {
      if (currentNodeList[i].nodeType != otherNodeList[i].nodeType) return false;
      if (currentNodeList[i].name != otherNodeList[i].name) return false;
      // if(currentNodeList[i].parentField != otherNodeList[i].parentField) return false;
      if (currentNodeList[i].codeInfo?.toStr() != otherNodeList[i].codeInfo?.toStr()) {
        return false;
      }
    }
    return true;
  }

  @override
  String toString() {
    return "[${node?.name ?? ""}]-泄露: 发生次数: $occurCount, 最近发生时间: $time, 泄露详情：\n${node.toString()}";
  }
}

class LeakList {
  List<LeakInfo>? leakInfo;

  LeakList() {
    leakInfo = [];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    if (leakInfo != null) {
      data['leakInfo'] = leakInfo!.map((v) => v.toJson()).toList();
    }
    return data;
  }

  LeakList.fromJson(Map<String, dynamic> json) {
    if (json['leakInfo'] != null) {
      leakInfo = <LeakInfo>[];
      json['leakInfo'].forEach((v) {
        leakInfo!.add(LeakInfo.fromJson(v));
      });
    }
  }
}
